<template>
  <div class="card-gallery">
    <!-- 使用 Element UI 的菜单组件 -->
    <el-menu
      :default-active="activeCategory"
      mode="horizontal"
      @select="handleSelect"
      class="category-menu"
    >
      <el-menu-item
        v-for="(category, index) in categories"
        :key="index"
        :index="category.name"
      >
        {{ category.name }}
      </el-menu-item>
    </el-menu>

    <!-- 小卡展示区 -->
    <div class="card-container">
      <el-card v-if="selectedCategory" class="gallery-card">
        <div slot="header" class="category-header">
          <h3>{{ selectedCategory.name }}</h3>
          <span class="card-count">{{ selectedCategory.cards.length }} 张小卡</span>
        </div>
        <el-row :gutter="20">
          <el-col
            v-for="(card, cardIndex) in selectedCategory.cards"
            :key="cardIndex"
            :xs="12" :sm="8" :md="6" :lg="4"
            class="card-item"
          >
            <div class="card-wrapper">
              <el-image
                :src="card.image"
                fit="cover"
                class="star-card"
                :preview-src-list="selectedCategory.cards.map(c => c.image)"
                :initial-index="cardIndex"
              >
                <div slot="placeholder" class="image-slot">
                  <i class="el-icon-picture-outline"></i>
                </div>
                <div slot="error" class="image-slot">
                  <i class="el-icon-picture-outline"></i>
                </div>
              </el-image>
              <div class="card-info">
                <span class="card-title">{{ card.title }}</span>
                <div class="card-meta">
                  <el-tag size="mini" type="info">{{ card.group }}</el-tag>
                  <el-tag size="mini">{{ card.member }}</el-tag>
                </div>
              </div>
            </div>
          </el-col>
        </el-row>
      </el-card>
      <el-empty v-else description="请选择一个分类" class="empty-state"></el-empty>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      categories: [
        {
          name: "BTS",
          cards: [
            {
              image: "https://th.bing.com/th/id/R.34fe0ca45a9e647597750e33f7e0042a?rik=G7TyvFOgQc%2ftIQ&riu=http%3a%2f%2fimg.netbian.com%2ffile%2f2024%2f0119%2f0044530iKJJ.jpg&ehk=EGIpvNmZEOLdFXsYREhlS5RkI5N9VJI8hR4RAUzDHsI%3d&risl=&pid=ImgRaw&r=0",
              title: "Jin 官方小卡",
              group: "BTS",
              member: "Jin",
              series: "LOVE YOURSELF"
            },
            {
              image: "https://via.placeholder.com/275x425/74b9ff/ffffff?text=BTS+Suga",
              title: "Suga 限定小卡",
              group: "BTS",
              member: "Suga",
              series: "MAP OF THE SOUL"
            },
            {
              image: "https://via.placeholder.com/275x425/55efc4/ffffff?text=BTS+RM",
              title: "RM 签名小卡",
              group: "BTS",
              member: "RM",
              series: "BE"
            },
            {
              image: "https://via.placeholder.com/275x425/a29bfe/ffffff?text=BTS+J-Hope",
              title: "J-Hope 舞台小卡",
              group: "BTS",
              member: "J-Hope",
              series: "MOTS:7"
            },
            {
              image: "https://via.placeholder.com/275x425/ffeaa7/ffffff?text=BTS+Jimin",
              title: "Jimin 特别版",
              group: "BTS",
              member: "Jimin",
              series: "LY: ANSWER"
            },
            {
              image: "https://via.placeholder.com/275x425/fd79a8/ffffff?text=BTS+V",
              title: "V 写真小卡",
              group: "BTS",
              member: "V",
              series: "WINGS"
            },
            {
              image: "https://via.placeholder.com/275x425/e17055/ffffff?text=BTS+JK",
              title: "JK 限量小卡",
              group: "BTS",
              member: "Jungkook",
              series: "DARK & WILD"
            },
          ],
        },
        {
          name: "BLACKPINK",
          cards: [
            {
              image: "https://via.placeholder.com/275x425/ff7675/ffffff?text=BP+Jisoo",
              title: "Jisoo 官方小卡",
              group: "BLACKPINK",
              member: "Jisoo",
              series: "THE ALBUM"
            },
            {
              image: "https://via.placeholder.com/275x425/74b9ff/ffffff?text=BP+Jennie",
              title: "Jennie 限定小卡",
              group: "BLACKPINK",
              member: "Jennie",
              series: "BORN PINK"
            },
            {
              image: "https://via.placeholder.com/275x425/55efc4/ffffff?text=BP+Rose",
              title: "Rose 签名小卡",
              group: "BLACKPINK",
              member: "Rose",
              series: "KILL THIS LOVE"
            },
            {
              image: "https://via.placeholder.com/275x425/a29bfe/ffffff?text=BP+Lisa",
              title: "Lisa 舞台小卡",
              group: "BLACKPINK",
              member: "Lisa",
              series: "SQUARE UP"
            },
          ],
        },
        {
          name: "TWICE",
          cards: [
            {
              image: "https://via.placeholder.com/275x425/ff7675/ffffff?text=TW+Nayeon",
              title: "Nayeon 官方小卡",
              group: "TWICE",
              member: "Nayeon",
              series: "FORMULA OF LOVE"
            },
            {
              image: "https://via.placeholder.com/275x425/74b9ff/ffffff?text=TW+Jeongyeon",
              title: "Jeongyeon 限定小卡",
              group: "TWICE",
              member: "Jeongyeon",
              series: "EYES WIDE OPEN"
            },
            {
              image: "https://via.placeholder.com/275x425/55efc4/ffffff?text=TW+Momo",
              title: "Momo 签名小卡",
              group: "TWICE",
              member: "Momo",
              series: "MORE & MORE"
            },
            {
              image: "https://via.placeholder.com/275x425/a29bfe/ffffff?text=TW+Sana",
              title: "Sana 舞台小卡",
              group: "TWICE",
              member: "Sana",
              series: "FEEL SPECIAL"
            },
            {
              image: "https://via.placeholder.com/275x425/ffeaa7/ffffff?text=TW+Jihyo",
              title: "Jihyo 特别版",
              group: "TWICE",
              member: "Jihyo",
              series: "FANCY YOU"
            },
            {
              image: "https://via.placeholder.com/275x425/fd79a8/ffffff?text=TW+Mina",
              title: "Mina 写真小卡",
              group: "TWICE",
              member: "Mina",
              series: "YES or YES"
            },
            {
              image: "https://via.placeholder.com/275x425/e17055/ffffff?text=TW+Dahyun",
              title: "Dahyun 限量小卡",
              group: "TWICE",
              member: "Dahyun",
              series: "WHAT IS LOVE?"
            },
            {
              image: "https://via.placeholder.com/275x425/00b894/ffffff?text=TW+Chaeyoung",
              title: "Chaeyoung 官方小卡",
              group: "TWICE",
              member: "Chaeyoung",
              series: "SIGNAL"
            },
            {
              image: "https://via.placeholder.com/275x425/0984e3/ffffff?text=TW+Tzuyu",
              title: "Tzuyu 限定小卡",
              group: "TWICE",
              member: "Tzuyu",
              series: "TWICETAGRAM"
            },
          ],
        },
      ],
      activeCategory: "BTS",
      selectedCategory: null,
    };
  },
  methods: {
    handleSelect(index) {
      this.activeCategory = index;
      this.selectedCategory = this.categories.find((cat) => cat.name === index);
    }
  },
  created() {
    this.selectedCategory = this.categories.find(
      (cat) => cat.name === this.activeCategory
    );
  },
};
</script>

<style scoped>
.card-gallery {
  max-width: 1200px;
  margin: 40px auto;
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
}

.category-menu {
  margin-bottom: 30px;
  display: flex;
  justify-content: center;
}

.card-container {
  margin-top: 10px;
}

.gallery-card {
  border-radius: 8px;
}

.category-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.category-header h3 {
  font-size: 16px;
  font-weight: 500;
  color: #333;
  margin: 0;
}

.card-count {
  font-size: 12px;
  color: #909399;
}

.card-item {
  margin-bottom: 20px;
  padding: 0 10px;
}

.card-wrapper {
  position: relative;
  border-radius: 6px;
  overflow: hidden;
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.card-wrapper:hover {
  transform: translateY(-5px);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.15);
}

.star-card {
  width: 100%;
  height: 0;
  padding-top: 154.5%; /* 8.5 / 5.5 ≈ 1.545 */
  display: block;
  position: relative;
}

.star-card >>> .el-image__inner {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.image-slot {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background: #f5f7fa;
  color: #909399;
  font-size: 30px;
  position: absolute;
  top: 0;
  left: 0;
}

.card-info {
  padding: 10px;
  background: #fff;
  border-top: 1px solid #f0f0f0;
}

.card-title {
  display: block;
  font-size: 13px;
  font-weight: 500;
  color: #333;
  margin-bottom: 5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.card-meta {
  display: flex;
  justify-content: space-between;
  margin-top: 5px;
}

.card-meta .el-tag {
  margin-right: 4px;
}

.empty-state {
  height: 300px;
  display: flex;
  justify-content: center;
  align-items: center;
  border: 1px dashed #eee;
  border-radius: 8px;
}

@media (max-width: 768px) {
  .card-item {
    margin-bottom: 15px;
  }

  .card-title {
    font-size: 12px;
  }

  .card-meta .el-tag {
    font-size: 10px;
    padding: 0 5px;
    height: 20px;
    line-height: 20px;
  }
}
</style>
